#include "sys.h"
#include "delay.h"
#include "usart.h"
#include "led.h"
#include "key.h"  
#include "CH9434.h"
#include "spi_ch9434.h"
#include "spi.h"
#include "oem.h"

 
uint8_t uart_idx;
uint8_t uart_iir;
uint8_t uart_lsr;
uint8_t uart_msr;

uint16_t rec_buf_cnt = 0;
uint8_t uart_rec_buf[512];

uint32_t uart_rec_total_cnt[4] = {0,0,0,0};



int main(void) 
{
	u8_t buf[20]="log gpgga\r\n";
	u8 key;
	
	OEM_Typedef OEM_Struct;
	
	NVIC_PriorityGroupConfig(NVIC_PriorityGroup_2);//设置系统中断优先级分组2
	delay_init(168);
	uart_init(115200);
	LED_Init();
	KEY_Init();
	InitIntGPIO();
	
	
	/* SPI传输测试 */
	/* 初始化CH9434 */
	printf("CH9434 TEST START %s %s \r\n",__TIME__,__DATE__);
	CH9434_Init();	//SPI初始化
	
	delay_ms(1000);
	
	CH9434CLKMode_Init();		//时钟初始化
	delay_ms(50);
	UsartConfig_Init();		//初始化串口配置
	
	
	printf("into while\n  ");
	while(1)
	{
		key = KEY_Scan(0);
		if(key == KEY0_PRES)
			CH9434_Send(CH9434_UART_IDX_0,"log gpzda\r\n");
		if(key == KEY1_PRES)
			CH9434_Send(CH9434_UART_IDX_0,"log m925b\r\n");
		if(key == KEY2_PRES)
			CH9434_Send(CH9434_UART_IDX_0,"log gpgga\r\n");
#if 1
		/* 串口线程处理 */
		if(GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_6)==Bit_RESET) //INT电平为低
		{
			for(uart_idx=0; uart_idx<4; uart_idx++)
			{
				uart_iir = CH9434UARTxReadIIR(uart_idx);
				//printf("idx:%d uart_iir:%02x\r\n",uart_idx,uart_iir);
				switch(uart_iir & 0x0f)
				{
					case 0x01://没有中断
						break;
					case 0x06://接收线路状态
					{
						uart_lsr = CH9434UARTxReadLSR(uart_idx);
						//printf("uart_lsr:%02x\r\n",uart_lsr);
						rec_buf_cnt = CH9434UARTxGetRxFIFOLen(uart_idx);
						if(rec_buf_cnt)
						{
							CH9434UARTxGetRxFIFOData(uart_idx,uart_rec_buf,rec_buf_cnt);
							//uart_rec_total_cnt[uart_idx] += rec_buf_cnt;
							//printf("idx:%d rec:%d total:%d\r\n",uart_idx,rec_buf_cnt,(int)uart_rec_total_cnt[uart_idx]);
							//CH9434UARTxSetTxFIFOData(uart_idx,uart_rec_buf,rec_buf_cnt); //回传串口发送过来的数据
						}	
						OEMMSG_Distinguish(&OEM_Struct,uart_rec_buf);
						printf("%s\n",uart_rec_buf);
						buf_bzero(uart_rec_buf,512);						
						break;
					}
					case 0x04://接收数据可用	
					{
						rec_buf_cnt = CH9434UARTxGetRxFIFOLen(uart_idx);
						if(rec_buf_cnt)
						{
							CH9434UARTxGetRxFIFOData(uart_idx,uart_rec_buf,rec_buf_cnt);
							//uart_rec_total_cnt[uart_idx] += rec_buf_cnt;
							//printf("idx:%d rec:%d total:%d\r\n",uart_idx,rec_buf_cnt,(int)uart_rec_total_cnt[uart_idx]);
							//CH9434UARTxSetTxFIFOData(uart_idx,uart_rec_buf,rec_buf_cnt); //回传串口发送过来的数据
						}	
						OEMMSG_Distinguish(&OEM_Struct,uart_rec_buf);
						printf("%s\n",uart_rec_buf);
						buf_bzero(uart_rec_buf,512);
						break;
					}
					case 0x0C://接收数据超时
					{
						rec_buf_cnt = CH9434UARTxGetRxFIFOLen(uart_idx);
						if(rec_buf_cnt)
						{
							CH9434UARTxGetRxFIFOData(uart_idx,uart_rec_buf,rec_buf_cnt);
							//uart_rec_total_cnt[uart_idx] += rec_buf_cnt;
							//printf("idx:%d rec:%d total:%d\r\n",uart_idx,rec_buf_cnt,(int)uart_rec_total_cnt[uart_idx]);
							//CH9434UARTxSetTxFIFOData(uart_idx,uart_rec_buf,rec_buf_cnt); //回传串口发送过来的数据
						}
						OEMMSG_Distinguish(&OEM_Struct,uart_rec_buf);
						printf("%s\n",uart_rec_buf);
						buf_bzero(uart_rec_buf,512);
						break;
					}
					case 0x02://THR寄存器空
						break;
					case 0x00://modem信号变化
					{
						uart_msr = CH9434UARTxReadMSR(uart_idx);
						printf("uart_msr:%02x\r\n",uart_msr);
						break;
					}
				}
			}
		}
#endif
		LED0=0;//提示系统正在运行
	}
}


